package com.mysimplenews.news.view;

import android.content.Context;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.mysimplenews.R;
import com.mysimplenews.news.model.NewsModel;
import com.mysimplenews.news.model.NewsModelImp;

import java.util.ArrayList;
import java.util.List;

public class NewsFragment extends Fragment implements NewsModelImp.OnLoadTabDataListListener{

    private ViewPager viewPager;
    private NewsModel newsModel;
    private MyViewPagerAdapter adapter;
    private TabLayout mTablayout;

    public NewsFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        newsModel = new NewsModelImp(getContext());
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_news, container, false);
        mTablayout = (TabLayout) view.findViewById(R.id.tab_layout);
        viewPager = (ViewPager) view.findViewById(R.id.viewpager);
        initPagerAdapter();
        initTab();
        newsModel.loadTabData(this);
        return view;
    }

    private void initTab() {
        mTablayout.setTabMode(TabLayout.MODE_SCROLLABLE);
    }

    private void initPagerAdapter() {
        adapter = new MyViewPagerAdapter(getFragmentManager());
        viewPager.setAdapter(adapter);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                System.out.println("@@@@@@@@========>" + position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    public void onCacheSuccess(List<String> tabs) {
        refreshTabUI(tabs);
    }

    @Override
    public void onNetSuccess(List<String> tabs) {
        refreshTabUI(tabs);
    }

    @Override
    public void onFailure(String msg, Exception e) {

    }

    private void refreshTabUI(List<String> tabs) {
        adapter.clear();
        mTablayout.removeAllTabs();
        for (int i = 0; i < tabs.size(); i++) {
            String title = tabs.get(i);
            adapter.addFragment(ListNewsFragment.newInstance(""),title);
            mTablayout.addTab(mTablayout.newTab().setText(title));
            System.out.println("!!!!!!!!!!!!!!" + title);
        }
        adapter.notifyDataSetChanged();
        mTablayout.setupWithViewPager(viewPager);
    }

    private class MyViewPagerAdapter extends FragmentPagerAdapter{

        private List<Fragment> fragments = new ArrayList<Fragment>();
        private List<String> titles = new ArrayList<String>();
        public MyViewPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        public void addFragment(Fragment fragment,String title){
            fragments.add(fragment);
            titles.add(title);
        }

        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }

        @Override
        public int getCount() {
            return fragments.size();
        }


        @Override
        public CharSequence getPageTitle(int position) {
            return titles.get(position);
        }

        public void clear(){
            fragments.clear();
            titles.clear();
        }
    }


    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

    @Override
    public void onDetach() {
        super.onDetach();
    }

}
